package com.lihaozhe.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.lihaozhe.constant.CommonConstant;
import com.lihaozhe.util.response.ResponseResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李昊哲
 * @version 1.0
 * 2021/11/28 12:38
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/test")
public class TestController {
    private final CommonConstant commonConstant;

    @GetMapping("/getCommonConstant")
    @JsonView(CommonConstant.class)
    public CommonConstant getCommonConstant() {
        log.info("getCommonConstant");
        log.info("title = {}", commonConstant.getTitle());
        log.info("slogan = {}", commonConstant.getSlogan());
        return commonConstant;
    }

    @Secured({"ROLE_admin","ROLE_user"})
    @GetMapping("/insert")
    public ResponseResult<String> insert(){
        ResponseResult result = ResponseResult.success();
        result.setData("insert");
        return result;
    }
    @PreAuthorize("hasAnyAuthority('ROLE_user')")
    @GetMapping("/delete")
    public ResponseResult<String> delete(){
        ResponseResult result = ResponseResult.success();
        result.setData("delete");
        return result;
    }
    @PreAuthorize("hasAnyRole('user')")
    @GetMapping("/update")
    public ResponseResult<String> update(){
        ResponseResult result = ResponseResult.success();
        result.setData("update");
        return result;
    }
}